package com.cm.sys.intermanager.controller;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cm.sys.framework.application.RequestParameter;
import com.cm.sys.framework.application.ResponseResult;
import com.cm.sys.framework.utils.JsonUtil;
import com.cm.sys.intermanager.service.LoggerService;
import com.cm.sys.intermanager.utils.HttpResult;

/***
 * 日志查询接口
 * 
 * @author yxq18
 * @date 2019/12/10
 */
@Controller
public class LoggerController {

    private static final Logger logger = LoggerFactory.getLogger(LoggerController.class);

    @Autowired
    private LoggerService loggerService;

    /***
     * 日志查询接口
     * 
     * @param rp
     * @return
     */
    @RequestMapping(value = "/logger", method = RequestMethod.POST)
    public ResponseResult getLogger(RequestParameter rp) {

        ResponseResult rr = new ResponseResult();
        JSONObject args = rp.getparameters();

        logger.info("==========日志查询接口的入参====【{}】===================", JSON.toJSONString(args));

        String businessName = JsonUtil.getString(args, "businessName", null);// 业务线

        String startTime = JsonUtil.getString(args, "startTime", null);
        String endTime = JsonUtil.getString(args, "endTime", null);
        String loggerType = JsonUtil.getString(args, "loggerType", null);
        // 查询业务处理层的接口，默认为1是当前页
        Integer pageIndex = JsonUtil.getInt(args, "pageIndex", 1);
        Integer pageSize = JsonUtil.getInt(args, "pageSize", 10);

        String transId = JsonUtil.getString(args, "transId", null);// 流水号

        String phoneNum = JsonUtil.getString(args, "phoneNum", null);

        JSONObject json = new JSONObject();

        json.put("pageIndex", pageIndex);
        json.put("pageSize", pageSize);
        json.put("fileType", loggerType);
        json.put("indexName", businessName);

        HttpResult hr = getLoggerInfo(json, transId, phoneNum, startTime, endTime);

        HashMap<String, Object> map = new HashMap<String, Object>(16);
        map.put("lists", StringUtils.isEmpty(hr) ? null : hr.getData());
        rr.setMap(map);
        return rr;
    }

    /****
     * 判断走哪个接口查询日志
     * 
     * @param json
     * @param transId
     * @param phoneNum
     * @param startTime
     * @param endTime
     * @return
     */
    private HttpResult getLoggerInfo(JSONObject json, String transId, String phoneNum, String startTime,
        String endTime) {

        HttpResult hr = null;

        if (!StringUtils.isEmpty(transId) && !StringUtils.isEmpty(phoneNum) && StringUtils.isEmpty(startTime)) {// 手机流水没有时间

            json.put("transId", transId);
            json.put("phoneNum", phoneNum);
            logger.info("==========日志查询手机加流水没有时间接口的参数====【{}】===================", json);
            hr = loggerService.getLoggerPhoneTransIdNoTime(json);
        } else if (StringUtils.isEmpty(phoneNum) && !StringUtils.isEmpty(transId) && StringUtils.isEmpty(startTime)) {// 流水号查询没有时间

            json.put("transId", transId);
            logger.info("==========日志流水没有时间查询接口的参数====【{}】===================", json);
            hr = loggerService.getLoggerTransIdNoTime(json);
        } else if (StringUtils.isEmpty(transId) && !StringUtils.isEmpty(phoneNum) && StringUtils.isEmpty(startTime)) {// 手机号码没有时间

            json.put("phoneNum", phoneNum);
            logger.info("==========日志查手机号码没有时间查询查询接口的参数====【{}】===================", json);
            hr = loggerService.getLoggerPhoneNoTime(json);
        } else if (!StringUtils.isEmpty(transId) && !StringUtils.isEmpty(phoneNum) && !StringUtils.isEmpty(startTime)) {// 手机流水加时间

            json.put("transId", transId);
            json.put("phoneNum", phoneNum);
            json.put("startTime", startTime);
            json.put("endTime", endTime);
            logger.info("==========日志查询手机流水加时间接口的参数====【{}】===================", json);
            hr = loggerService.getLoggerPhoneTransId(json);
        } else if (StringUtils.isEmpty(phoneNum) && !StringUtils.isEmpty(transId) && !StringUtils.isEmpty(startTime)) {// 流水号查询加时间

            json.put("transId", transId);
            json.put("startTime", startTime);
            json.put("endTime", endTime);
            logger.info("==========日志流水查询接口的参数====【{}】===================", json);
            hr = loggerService.getLoggerTransId(json);
        } else if (StringUtils.isEmpty(transId) && !StringUtils.isEmpty(phoneNum) && !StringUtils.isEmpty(startTime)) {// 手机号码加时间

            json.put("phoneNum", phoneNum);
            json.put("startTime", startTime);
            json.put("endTime", endTime);
            logger.info("==========日志查手机号码查询查询接口的参数====【{}】===================", json);
            hr = loggerService.getLoggerPhone(json);
        } else if (StringUtils.isEmpty(transId) && StringUtils.isEmpty(phoneNum) && !StringUtils.isEmpty(startTime)) {// 时间查询

            json.put("startTime", startTime);
            json.put("endTime", endTime);
            logger.info("==========日志时间查询查接口的参数====【{}】===================", json);
            hr = loggerService.getLoggerTime(json);
        }

        return hr;

    }

}
